<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Upload extends Base_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->model('upload_m');
        $this->load->model('attachs_m');
        $this->load->model('order_m');
    }

    public function upload_book()
    {
        $access_key = $this->input->get('access_key', true);
        if (empty($access_key)) {
            $access_key = md5($this->session->userdata('user_id') . time() . uniqid());
        }
        $arr                     = [];
        $config['upload_path']   = FCPATH . 'uploads/books/' . $access_key . "/";
        $config['allowed_types'] = 'gif|jpg|png';
        $config['encrypt_name']  = true;
        $config['max_size']      = 3000;
        if (!file_exists($config['upload_path'])) {
            mkdir($config['upload_path'], 0777, true);
        }
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('file')) {
            $arr = ['status' => 4, 'msg' => '图片上传失败'];
        }
        $upload_data              = $this->upload->data();
        $attachs['file_name']     = $upload_data['file_name'];
        $attachs['access_key']    = $access_key;
        $attachs['add_time']      = time();
        $attachs['file_location'] = 'uploads/books/' . $access_key . "/";
        $attachs['is_image']      = 1;
        $attachs['item_type']     = 1;//1为客服留言用
        $this->attachs_m->add_attachs($attachs);
        $url = base_url('uploads/books/' . $access_key . '/' . $upload_data['file_name']);
        $arr = ['status' => 1, 'msg' => '图片上传成功', 'url' => $url];
        echo json_encode($arr);
    }

    public function upload_order()
    {
        $order = $this->input->get('order', true);
        if (empty($order)) {
            $arr = ['status' => 2, 'msg' => '图片上传失败'];
        }
        $order_info = $this->order_m->get_order_by_id($order);
        if ($order_info['order_lock'] > 1) {
            echo json_encode(['status' => 2, "msg" => "此单已经处理过,上传失败"]);
            exit();
        }
        $arr                     = [];
        $config['upload_path']   = FCPATH . 'uploads/order/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['file_name']     = $order . '.jpg';
        $config['encrypt_name']  = false;
        $config['overwrite']     = true;
        $config['max_size']      = 3000;
        if (!file_exists($config['upload_path'])) {
            mkdir($config['upload_path'], 0777, true);
        }
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('file')) {
            $arr = ['status' => 4, 'msg' => '图片上传失败'];
        }
        $upload_data              = $this->upload->data();
        $attachs['file_name']     = $upload_data['file_name'];
        $attachs['access_key']    = $order;
        $attachs['add_time']      = time();
        $attachs['file_location'] = 'uploads/order/' . $order . "/";
        $attachs['is_image']      = 1;
        $attachs['item_type']     = 2;//2为订单凭证
        $this->attachs_m->add_attachs($attachs);
        $url = base_url('uploads/order/' . $upload_data['file_name'] . '?t=' . time());
        $arr = ['status' => 1, 'msg' => '图片上传成功', 'url' => $url];
        echo json_encode($arr);
    }

    public function upload_card()
    {
        $uid       = $this->input->get('uid', true);
        $name      = $this->input->get('name', true);
        $user_info = $this->user_m->get_user_by_user_id($uid);
        if (empty($uid)) {
            $uid = $this->session->userdata('user_id');
        }
        $arr = [];
        if ($user_info['user_card_rz'] == 2) {
            $arr = ['status' => 4, 'msg' => '您已经认证，修改失败'];
        } else {
            $config['upload_path']   = FCPATH . 'uploads/card/' . $uid . "/";
            $config['allowed_types'] = 'gif|jpg|png|JPG|PNG|JPEG|GIF|bmp|jpeg';
            $config['file_name']     = $name . '.jpg';
            $config['encrypt_name']  = false;
            $config['overwrite']     = true;
            $config['max_size']      = 3000;

            if (!file_exists($config['upload_path'])) {
                mkdir($config['upload_path'], 0777, true);
            }
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('' . $name . '')) {
                $arr = ['status' => 4, 'msg' => '图片上传失败'];
            } else {
                $upload_data = $this->upload->data();
                $url         = base_url('uploads/card/' . $uid . '/' . $upload_data['file_name'] . '?t=' . time());
                $arr         = ['status' => 1, 'msg' => '图片上传成功', 'url' => $url];
            }
        }
        echo json_encode($arr);
    }

    /**
     * 删除图片
     * [deltet_img id]
     * @return [type] [description]
     */
    public function delete_img()
    {
        $attach_id = $this->input->post('key', true);
        if ($attach_id) {
            $pic = $this->attachs_m->get_attachs_by_id($attach_id);
            if (file_exists(FCPATH . $pic['file_location'] . $pic['file_name'])) {
                unlink(FCPATH . $pic['file_location'] . $pic['file_name']);
            }
            $del_res = $this->attachs_m->delete_attachs_by(['id' => $attach_id]);
            $arr     = ['status' => 1, 'msg' => '图片删除成功'];
            echo json_encode($arr);
            exit;
        } else {
            return false;
        }
    }

    /**
     * 删除图片
     * [deltet_img id]
     * @return [type] [description]
     */
    public function delete_img_new()
    {
        $attach_id = $this->input->post('key', true);
        $url       = $this->input->get('url', true);
        $filePath  = parse_url($url);
        if ($attach_id) {
            if (file_exists(FCPATH . $filePath['path'])) {
                unlink(FCPATH . $filePath['path']);
            }
            $arr = ['status' => 1, 'msg' => '图片删除成功'];
            echo json_encode($arr);
            exit;
        } else {
            return false;
        }
    }

    public function upload_avatar()
    {
        $uid       = $this->input->get('uid', true);
        $name      = $this->input->get('name', true);
        $user_info = $this->user_m->get_user_by_user_id($uid);
        if (empty($uid)) {
            $uid = $this->session->userdata('user_id');
        }
        $config['upload_path']   = FCPATH . 'uploads/avatar/' . $uid . "/";
        $config['allowed_types'] = 'gif|jpg|png|JPG|PNG|JPEG|GIF|bmp|jpeg';
        $config['file_name']     = $name . '.jpg';
        $config['encrypt_name']  = false;
        $config['overwrite']     = true;
        $config['max_size']      = 3000;

        if (!file_exists($config['upload_path'])) {
            mkdir($config['upload_path'], 0777, true);
        }
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('avatar_pic')) {
            $arr = ['status' => 4, 'msg' => '图片上传失败'];
        } else {
            $upload_data = $this->upload->data();
            // 裁剪图片
            $this->load->library('Avatar');
            $this->avatar->resize_avatar($upload_data['full_path']);
            $data = [
                'user_avatar' => base_url('uploads/avatar/' . $uid . '/' . $name . '_middle.jpg?t=' . time()),
            ];
            $this->user_m->update_user($uid, $data);
            //删除tmp下的原图
            unlink($upload_data['full_path']);
            $url = base_url('uploads/avatar/' . $uid . '/' . $name . '_middle.jpg?t=' . time());
            $arr = ['status' => 1, 'msg' => '图片上传成功', 'url' => $url];
        }
        echo json_encode($arr);
    }

    public function upload_goods()
    {
        $uid  = $this->input->get('uid', true);
        $name = $this->input->get('name', true);
        $time = time();
        if (empty($uid)) {
            $uid = $this->session->userdata('user_id');
        }
        $config['upload_path']   = FCPATH . 'uploads/goods/' . $uid . "/" . $time . "/";
        $config['allowed_types'] = 'gif|jpg|png|JPG|PNG|JPEG|GIF|bmp|jpeg';
        $config['file_name']     = $name . '.jpg';
        $config['encrypt_name']  = false;
        $config['overwrite']     = true;
        $config['max_size']      = 3000;

        if (!file_exists($config['upload_path'])) {
            mkdir($config['upload_path'], 0777, true);
        }
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('' . $name . '')) {
            $arr = ['status' => 4, 'msg' => '图片上传失败'];
        } else {
            $upload_data = $this->upload->data();
            $url         = '/uploads/goods/' . $uid . '/' . $time . "/" . $upload_data['file_name'];
            $arr         = ['status' => 1, 'msg' => '图片上传成功', 'url' => $url];
        }
        echo json_encode($arr);
    }

    public function upload_file()
    {

        $uid = $this->session->userdata('user_id');

        $now                     = time();
        $config['upload_path']   = FCPATH . 'uploads/file/' . $now . "/";
        $config['allowed_types'] = '*';
        $config['encrypt_name']  = false;
        $config['overwrite']     = true;
        $config['max_size']      = 10000;

        if (!file_exists($config['upload_path'])) {
            mkdir($config['upload_path'], 0777, true);
        }
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('file')) {
            $arr = ['status' => 4, 'msg' => '图片上传失败'];
        } else {
            $upload_data = $this->upload->data();
            $url         = base_url('uploads/file/' . $now . '/' . $upload_data['file_name'] . '?t=' . time());
            $arr         = [
                'status' => 1,
                'msg'    => '图片上传成功',
                'name'   => $upload_data['file_name'],
                'url'    => $url,
                "path"   => 'uploads/file/' . $now . '/' . $upload_data['file_name'],
            ];
        }
        echo json_encode($arr);
        exit();
    }

    /**
     * 后台通用上传
     * @author fengchen@addcn.com
     * @date   2019/9/9
     */
    public function do_upload()
    {
        $type                    = $this->input->get('type', true);
        $config['upload_path']   = FCPATH . 'uploads/' . $type . '/' . date("Ymd") . "/";
        $config['allowed_types'] = 'gif|jpg|png';
        $config['encrypt_name']  = true;
        $config['max_size']      = 3000;
        if (!file_exists($config['upload_path'])) {
            mkdir($config['upload_path'], 0777, true);
        }
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('file')) {
            echo json_encode([
                'status' => 4,
                'msg'    => '图片上传失败',
            ]);
            exit();
        }
        $upload_data = $this->upload->data();
        $url         = '/uploads/' . $type . '/' . date("Ymd") . '/' . $upload_data['file_name'];
        $arr         = [
            'status' => 1,
            'msg'    => '图片上传成功',
            'url'    => $url,
        ];
        echo json_encode($arr);
        exit();
    }
}
